package com.jy.wms.common.base;



import net.sf.json.JSONObject;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

public class MD5 {
	// 获得MD5摘要算法的 MessageDigest 对象
	private static MessageDigest _mdInst = null;
	private static char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
	private static MessageDigest getMdInst() {
		if (_mdInst == null) {
			try {
				_mdInst = MessageDigest.getInstance("MD5");
			} catch (NoSuchAlgorithmException e) {
				e.printStackTrace();
			}
		}
		return _mdInst;
	}

	public final static String encode(String s) {
		try {
			byte[] btInput = s.getBytes("utf-8");
			// 使用指定的字节更新摘要
			getMdInst().update(btInput);
			// 获得密文
			byte[] md = getMdInst().digest();
			// 把密文转换成十六进制的字符串形式
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static void main(String[] arg){
		JSONObject json = new JSONObject();
		/*json.put("partnerId","双迪2");//申通
		json.put("net","辽宁大连得胜营业部");//申通
		json.put("partnerKey","sd123456*");//申通*/
		json.put("partnerId","K41154204");//圆通
		json.put("partnerKey","IYR6myDx");//圆通
		json.put("net","大连怡亚通");//圆通
		json.put("kuaidicom","yuantong");
		json.put("kuaidinum","");
		json.put("orderId","");
		JSONObject recMan = new JSONObject();
		recMan.put("name","斯奈克");
		recMan.put("mobile","15726153280");
		recMan.put("province","山东省");
		recMan.put("city","济南市");
		recMan.put("district","天桥区");
		recMan.put("addr","历山北路");
		recMan.put("printAddr","黄台电子商务产业园B座801");
		recMan.put("company","山东乐速信息科技有限公司");
		json.put("recMan",recMan);
		JSONObject sendMan = new JSONObject();
		sendMan.put("name","张益达");
		sendMan.put("mobile","15726153280");
		sendMan.put("province","山东省");
		sendMan.put("city","济南市");
		sendMan.put("district","高新区");
		sendMan.put("addr","会展中心");
		sendMan.put("printAddr","会展中心2031");
		sendMan.put("company","张益达的益达");
		json.put("sendMan",sendMan);
		json.put("cargo","");
		json.put("count","1");
		json.put("weight","0.4");
		json.put("volumn","1");
		json.put("payType","");
		json.put("expType","标准快递");
		json.put("remark","");
		json.put("valinsPay",new Double(0));
		json.put("collection",new Double(0));
		json.put("needChild","0");
		json.put("needBack","0");
		json.put("needTemplate","1");

		Long t = 1522235109775L;
		String key = "IGrimTsw8590";
		String secret = "e4d2bc8335c44b09a107ab0b7df26faa";



		Map<String, Object> params = new HashMap<String, Object>();
		System.out.println("原文："+ json.toString() + t + key + secret);
		String sign = MD5.encode(json.toString()+ t + key + secret);
		System.out.println("sign:"+sign);
		params.put("method","getElecOrder");
		params.put("param",json.toString());
		params.put("sign",sign);
		params.put("t",t.toString());
		params.put("key",key);
		String resp;
		System.out.println(params.toString());
		try {
			resp = HttpUtil.doPost("http://api.kuaidi100.com/eorderapi.do",params);
			System.out.println(resp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

//		try {
//			String date = "2018-02-28T16:00:00.000Z";
//			date = date.replace("Z", " UTC");//注意是空格+UTC
//			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");//注意格式化的表达式
//			Date d = format.parse(date );
//			SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM");//注意格式化的表达式
//			System.out.println(format1.format(d).toString());
//		} catch (ParseException e) {
//			e.printStackTrace();
//		}
	}
}
